package com.cmwa.scbp.sys.permission.operRole.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.scbp.base.mvc.controller.BaseController;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.Result;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.base.exception.RepositoryException;
import com.cmwa.scbp.sys.permission.dataAuthority.entity.RoleVo;
import com.cmwa.scbp.sys.permission.operRole.entity.OperRoleVo;
import com.cmwa.scbp.sys.permission.operRole.service.OperRoleService;

@Controller
@RequestMapping(value = "/sys/oper/role")
public class OperRoleController extends BaseController {
	/**
	 * 设置jsp的访问路径
	 */
	private final static String PAGE_PATH = "system/sys/permission/operRole/"; 
	@Resource
	private OperRoleService operRoleService;
	/**
	 * 角色列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/roleListView")
	public String roleListView() {
		return PAGE_PATH + "operRoleList";
	}
	
	/**
	 * 列表页面数据
	 * @param sp
	 * @return
	 */
	@RequestMapping("/roleListPage")
	@ResponseBody
	public Page<OperRoleVo> roleListPage(SearchParam sp) {
		Page<OperRoleVo> list = operRoleService.roleListPage(sp);
		return list;
	}
	
	/**
	 * 角色详情页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/roleDetailView")
	public String roleDetailView(@RequestParam("roleId") String roleId,ModelMap model) {
		OperRoleVo roleVo = operRoleService.queryRoleById(roleId);
		model.addAttribute("roleVo", roleVo);
		//List<RoleVo> roleList = operRoleService.getDataRole(roleId);
		//model.addAttribute("roleList", roleList);
		return PAGE_PATH + "operRoleDetail";
	}
	
	/**
	 * 获取详情页的数据角色列表
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/roleDetailListPage")
	@ResponseBody
	public Page<RoleVo> roleDetailListPage(SearchParam param) {
		Page<RoleVo> list = operRoleService.roleDetailListPage(param);
		return list;
	}
	
	/**
	 * 检查是否存在重复的角色名称
	 * @param menuNM
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/checkRepeatRole")
	@ResponseBody
	public Result<Object> checkRepeatRole(@RequestParam("roleName") String roleName ,@RequestParam(value="roleId",required=false) String roleId) throws Exception {
		boolean isRepeat = operRoleService.checkRepeatRole(roleName,roleId);
		Result<Object> result = new Result<Object>();
		result.setObject(isRepeat);
		return result;
	}
	
	/**
	 * 新增角色页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/roleAddView")
	public String roleAddView() {
		return PAGE_PATH + "operRoleAdd";
	}
	
	/**
	 * 保存角色数据
	 * @param roleVo
	 * @return
	 * @throws RepositoryException
	 */
	@RequestMapping("/saveRole")
	public String saveRole(OperRoleVo roleVo) throws RepositoryException {
		operRoleService.saveRole(roleVo);
		return redirectSuccess();
	}
	
	/**
	 * 修改角色页面
	 * @param menuId
	 * @param model
	 * @return
	 */
	@RequestMapping("/roleUpdateView")
	public String roleUpdateView(@RequestParam("roleId") String roleId, ModelMap model) {
		OperRoleVo roleVo = operRoleService.queryRoleById(roleId);
		model.addAttribute("roleVo", roleVo);
		return PAGE_PATH + "operRoleUpdate";
	}
	
	/**
	 * 更新保存角色数据
	 * @param menu
	 * @return
	 * @throws RepositoryException
	 */
	@RequestMapping("/updateRole")
	public String updateRole(OperRoleVo roleVo) throws RepositoryException {
		operRoleService.updateRole(roleVo);
		return redirectSuccess();
	}
	
	/**
	 * 删除角色菜单
	 * @param menuId
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/deleteRole")
	@ResponseBody
	public String deleteRole(@RequestParam("roleId") String roleId) throws Exception {
		operRoleService.deleteRoleWithStatus(roleId);
		return "success";
	}
}
